package com.xueyi.rebar.delivery.controller;

import com.xueyi.common.core.utils.StringUtils;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import com.xueyi.common.message.mq.callback.message.CallbackChannelMessage;
import com.xueyi.common.message.mq.callback.message.CallbackStreamMessage;
import com.xueyi.common.message.mq.callback.producer.CallbackProducer;
import com.xueyi.common.security.annotation.Logical;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.security.auth.Auth;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.rebar.delivery.domain.dto.RebarDistributionDeliveryOrderDto;
import com.xueyi.rebar.delivery.service.IRebarDistributionDeliveryOrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

import static com.xueyi.common.core.pojo.CommonResult.success;

/**
 * 配送单管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/order")
public class RebarDistributionDeliveryOrderController extends BaseController<RebarDistributionDeliveryOrderDto, IRebarDistributionDeliveryOrderService> {

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "配送单";
    }

    @Resource
    private CallbackProducer callbackProducer;

    /**
     * 查询配送单列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.REBAR_DISTRIBUTION_DELIVERY_ORDER_LIST)
    public AjaxResult list(RebarDistributionDeliveryOrderDto distributionDeliveryOrder) {
        return super.list(distributionDeliveryOrder);
    }

    @PutMapping("/approveTest")
    public AjaxResult approveTaskTest() {
        //taskService.approveTask(getLoginUserId(), reqVO);
        CallbackChannelMessage message1 = new CallbackChannelMessage();
        message1.setEventCode("200");
        message1.setEventCallbackSysNo(200);
        message1.setMessage("ZC通过1！from rebar");

        CallbackStreamMessage message2 = new CallbackStreamMessage();
        message2.setEventCode("200");
        message2.setEventCallbackSysNo(200);
        message2.setMessage("ZC通过2！from rebar");
        callbackProducer.sendChannelCallbackMessage(message1);
        callbackProducer.sendStreamCallbackMessage(message2);
        return super.success();
    }

    /**
     * 查询配送单详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.REBAR_DISTRIBUTION_DELIVERY_ORDER_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 配送单导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.REBAR_DISTRIBUTION_DELIVERY_ORDER_EXPORT)
    @Log(title = "配送单管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, RebarDistributionDeliveryOrderDto distributionDeliveryOrder) {
        super.export(response, distributionDeliveryOrder);
    }

    /**
     * 配送单新增
     */
    @Override
    @PostMapping("/add")
    @RequiresPermissions(Auth.REBAR_DISTRIBUTION_DELIVERY_ORDER_ADD)
    @Log(title = "配送单管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated @RequestBody RebarDistributionDeliveryOrderDto distributionDeliveryOrder) {
        return super.add(distributionDeliveryOrder);
    }

    /**
     * 配送单新增
     */
    @PostMapping("/submit")
    @RequiresPermissions(Auth.REBAR_DISTRIBUTION_DELIVERY_ORDER_ADD)
    @Log(title = "配送单管理", businessType = BusinessType.INSERT)
    public AjaxResult submit(@Validated @RequestBody RebarDistributionDeliveryOrderDto distributionDeliveryOrder) {
        String processInstanceId = this.baseService.submit(distributionDeliveryOrder);

        if(StringUtils.isNotEmpty(processInstanceId)){
            distributionDeliveryOrder.setCurrentFlowInstanceId(processInstanceId);
            distributionDeliveryOrder.setAuditStatus(1);
            super.edit(distributionDeliveryOrder);
            return super.success("配送单流程发起成功！");
        }else{
            return super.error("配送单流程发起失败！");
        }
    }

    /**
     * 配送单修改
     */
    @Override
    @PutMapping("/edit")
    @RequiresPermissions(Auth.REBAR_DISTRIBUTION_DELIVERY_ORDER_EDIT)
    @Log(title = "配送单管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated @RequestBody RebarDistributionDeliveryOrderDto distributionDeliveryOrder) {
        return super.edit(distributionDeliveryOrder);
    }

    /**
     * 配送单修改状态
     */
    @Override
    @PutMapping("/status")
    @RequiresPermissions(value = "Auth.REBAR_DISTRIBUTION_DELIVERY_ORDER_ES", logical = Logical.OR)
    @Log(title = "配送单管理", businessType = BusinessType.UPDATE_STATUS)
    public AjaxResult editStatus(@RequestBody RebarDistributionDeliveryOrderDto distributionDeliveryOrder) {
        return super.editStatus(distributionDeliveryOrder);
    }

    /**
     * 配送单批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.REBAR_DISTRIBUTION_DELIVERY_ORDER_DEL)
    @Log(title = "配送单管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取配送单选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 配送单管理 - 列表 */
        String REBAR_DISTRIBUTION_DELIVERY_ORDER_LIST = "rebar:order:list";
        /** 系统 - 配送单管理 - 详情 */
        String REBAR_DISTRIBUTION_DELIVERY_ORDER_SINGLE = "rebar:order:single";
        /** 系统 - 配送单管理 - 新增 */
        String REBAR_DISTRIBUTION_DELIVERY_ORDER_ADD = "rebar:order:add";
        /** 系统 - 配送单管理 - 修改 */
        String REBAR_DISTRIBUTION_DELIVERY_ORDER_EDIT = "rebar:order:edit";
        /** 系统 - 配送单管理 - 修改状态 */
        String[] REBAR_DISTRIBUTION_DELIVERY_ORDER_ES = {"rebar:order:edit", "rebar:order:es"};
        /** 系统 - 配送单管理 - 删除 */
        String REBAR_DISTRIBUTION_DELIVERY_ORDER_DEL = "rebar:order:delete";
        /** 系统 - 配送单管理 - 导出 */
        String REBAR_DISTRIBUTION_DELIVERY_ORDER_EXPORT = "rebar:order:export";
    }
}
